package kp.rpc.server.tcp;

import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import kp.rpc.server.HttpServer;
import kp.rpc.server.handler.TcpServerHandler;

public class VertxTcpServer implements HttpServer {
    private byte[] handleRequest(byte[] requestData) {
        return "hello world".getBytes();
    }

    @Override
    public void doStart(int port) {
        // 创建vertx实例
        Vertx vertx = Vertx.vertx();
        // 创建 netServer TCP 服务器
        NetServer netServer = vertx.createNetServer();
        //处理请求
        netServer.connectHandler(new TcpServerHandler());
        //启动监听端口
        netServer.listen(port, result -> {
            if (result.succeeded()) {
                System.out.println("Server is now listening on port " + port);
            } else {
                System.err.println("Failed to start server: " + result.cause());
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpServer().doStart(8101);
    }
}
